package com.hunanwenli.controller.register;

import com.hunanwenli.entity.UserInfo;
import com.hunanwenli.entity.UserPwd;
import com.hunanwenli.email.SendSms;
import com.hunanwenli.service.RedisService;
import com.hunanwenli.service.UserInfoService;
import com.hunanwenli.service.UserPwdService;
import com.hunanwenli.tool.ThreadPoolService;
import com.hunanwenli.tool.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;


/**
 * @author wanzhenyu
 * @date 2021/5/30 17:02
 */
@Controller
@RequestMapping(value = "/register")
public class registerController {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private UserPwdService userPwdService;
    @Autowired
    private SendSms sendSms;

   @PostMapping(value = "/fillPwd/{email}/{name}/{phone}/{pwd}")
   @ResponseBody
   public boolean fillPwd(@PathVariable("email") String email,
                          @PathVariable("name") String name,
                          @PathVariable("phone") String phone,
                          @PathVariable("pwd") String pwd){
       String isHavaUserRedis = redisService.get(phone);
       UserInfo isHavaUser = userInfoService.selectByPhone(phone);
       if (isHavaUser!=null||isHavaUserRedis!=null){
           return false;
       }
       Integer id = userInfoService.selectMaxId()+1;
       UserInfo userInfo = new UserInfo();
       Date date = new Date();
       userInfo.setEmail(email);
       userInfo.setName(name);
       userInfo.setPhone(phone);
       userInfo.setModified(date);
       userInfo.setBuildtime(date);
       userInfo.setId(id);
       String password = Tools.getMD5(pwd);
       UserPwd userPwd = new UserPwd();
       userPwd.setId(id);
       userPwd.setPassword(password);
       userPwd.setModified(date);
       int i = userInfoService.insert(userInfo);
       int j = userPwdService.insert(userPwd);
       if (j!=1||i!=1){
          return false;
       }else {
           String userInfoJson = Tools.objectToJson(userInfo);
           redisService.put(phone,userInfoJson);
           return true;
       }

   }


    @GetMapping(value = "/ajaxEmail/{email}")
    @ResponseBody
    public String ajaxEmail(@PathVariable("email") String email){
        UserInfo userInfo = userInfoService.selectByEmail(email);
        if (userInfo==null){
            return "true";
        }
        return "false";
    }

    @GetMapping(value = "/ajaxYanZhenMa/{code}/{email}")
    @ResponseBody
    public String ajaxYanZhenMa(@PathVariable("code") String code,
                                @PathVariable("email") String email) throws InterruptedException {
        String s = redisService.get(code);
        if (s!=null){
            return "请不要重复发送";
        }
        ThreadPoolExecutor executor = ThreadPoolService.getInstance();
        //验证码生成
        String securityCode = Tools.getSecurityCode();
        //多线程发送验证码到redis
        executor.execute(()->{
            redisService.put(code,securityCode,120L);
        });
        //多线程发送邮件
        executor.execute(()->{
            sendSms.sendSimpleMail(email,"验证码","您的验证码是："+securityCode);
        });
        return "已发送！！！";

    }



    @GetMapping(value = "/ajaxYanZhenMa2/{code}/{codeval}")
    @ResponseBody
    public String ajaxYanZhenMa2(@PathVariable("code") String code,
                                 @PathVariable("codeval") String codeval){
        String str = redisService.get(code);
        if (codeval.equals(str)){
            return "success";
        }else {
            return "failure";
        }
    }
    @GetMapping(value = "/ajaxPhone/{phone}")
    @ResponseBody
    public String ajaxPhone(@PathVariable("phone") String phone){
        UserInfo userInfo = userInfoService.selectByPhone(phone);
        if (userInfo==null){
            return "true";
        }
        return "false";
    }



}
